package com.ae.controller;

import com.ae.pojo.Course;
import com.ae.pojo.Subject;
import com.ae.pojo.Video;
import com.ae.service.CourseService;
import com.ae.service.SubjectService;
import com.ae.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

/**
 * @author bug-termination
 * @date 2020-10-19 16:04
 */
@Controller
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private VideoService videoService;

    @RequestMapping("course/{sId}")
    public ModelAndView findCourseBySubjectId(@PathVariable Integer sId) {
        List<Course> courseList = courseService.findCourseBySubjectId(sId);
        Subject subject = subjectService.selectByPrimaryKey(sId);
        for (Course course : courseList) {
            List<Video> videoList = videoService.findByCourseId(course.getId());
            course.setVideoList(videoList);
        }
        subject.setCourseList(courseList);

        List<Subject> subjectList = subjectService.selectAll();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("subject", subject);
        modelAndView.addObject("subjectList", subjectList);
        modelAndView.setViewName("/before/course.jsp");
        return modelAndView;
    }
}